package com.chaoyong.higo.activity;

import java.util.List;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.adapter.ReceiptAddressAdapter;
import com.chaoyong.higo.adapter.ReceiptAddressAdapter.OnAddressDeleteListener;
import com.chaoyong.higo.adapter.ReceiptAddressAdapter.OnDefaultAddressChange;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.bean.GetAddressInfoBean;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.JLog;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.V;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/***
 * 收货地址
 * 
 * @author zk
 *
 */
public class ReceiptAddressActivity extends BaseActivity {

	private static final String ClassName = LoginActivity.class.getName();

	private LinearLayout base_left_iv;// 左侧返回按钮
	private TextView base_title_tv;// 标题
	private TextView base_right_tv;// 标题右侧文字

	private ListView safe_list_view;
	private ReceiptAddressAdapter adapter;

	private Button adress_add_new_btn;

	List<GetAddressInfoBean.DataEntity> entity;

	@Override
	public int layoutResID() {
		return R.layout.activity_account_safe;
	}

	@Override
	public void findView() {
		findTitleIds();

		adress_add_new_btn = V.f(this, R.id.adress_add_new_btn);
	}

	private void findTitleIds() {
		base_left_iv = V.f(this, R.id.base_left_iv);
		base_title_tv = V.f(this, R.id.base_title_tv);
		base_right_tv = V.f(this, R.id.base_right_tv);

		base_title_tv.setText("收货地址");
		base_right_tv.setVisibility(View.GONE);
	}

	@Override
	public void init() {
		base_left_iv.setOnClickListener(this);
		adress_add_new_btn.setOnClickListener(this);

		safe_list_view = V.f(this, R.id.safe_list_view);
		adapter = new ReceiptAddressAdapter(this, new OnDefaultAddressChange() {

			@Override
			public void onDefaulAdsChange(String adsId) {
				//设为默认地址
				if (!EmptyUtil.isEmpty(adsId)) {
					httpSetDefault(PrefUtils.getString(ReceiptAddressActivity.this, "userId", "1"), adsId);
				}
			}
		}, new OnAddressDeleteListener() {
			
			@Override
			public void onAddressDelete(String adsId) {
				//删除地址
				if (!EmptyUtil.isEmpty(adsId)) {
					httpDeleteAddress(adsId);
					finish();
					startActivity(new Intent(ReceiptAddressActivity.this, ReceiptAddressActivity.class));
					
					//adapter.notifyDataSetChanged();
				}
			}
		});
		
		safe_list_view.setAdapter(adapter);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.base_left_iv:// title左侧返回
			this.finish();
			break;
		case R.id.adress_add_new_btn:// 添加新地址
			startActivity(new Intent(this, AddReceiptAddressActivity.class));
			break;
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		String uid = PrefUtils.getString(ReceiptAddressActivity.this, "userId", "1");
		httpGetAddressInfo(uid);
	}

	/**
	 * 获取收货地址
	 * 
	 * @param uid 用户id
	 */
	private void httpGetAddressInfo(String uid) {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();

			JSONObject info = new JSONObject();
			info.putOpt("uid", uid);

			jsonObject.putOpt("model", "MemberAddress");
			jsonObject.putOpt("function", "SiteApp");
			jsonObject.putOpt("info", info);

			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		utils.send(HttpMethod.POST, OApi.Appport_appMemberInfo, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				JLog.e(ClassName, arg0.getMessage());
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				JLog.i(ClassName, arg0.result);
				Gson gson = new Gson();
				String result = arg0.result;
				try {
					JSONObject jso = new JSONObject(result);
					if(jso.getInt("status") == 1){
						GetAddressInfoBean addressInfo = gson.fromJson(arg0.result, GetAddressInfoBean.class);
						String status = addressInfo.getStatus();
						if (status.equals("1")) {
							entity = addressInfo.getData();
							if (!EmptyUtil.isEmpty(entity)) {
								adapter.setList(entity);
							}
						}
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
	}

	/**
	 * 设为默认地址
	 * 
	 * @param uid
	 *            用户id
	 * @param id
	 *            地址自增id
	 */
	private void httpSetDefault(String uid, String id) {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();

			JSONObject info = new JSONObject();
			info.putOpt("uid", uid);
			info.putOpt("id", id);

			jsonObject.putOpt("model", "MemberAddress");
			jsonObject.putOpt("function", "insertAddApp");
			jsonObject.putOpt("info", info);

			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		utils.send(HttpMethod.POST, OApi.Appport_appMemberInfo, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				JLog.e("oshop", arg0.getMessage().toString());
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				httpGetAddressInfo(PrefUtils.getString(ReceiptAddressActivity.this, "userId", "1"));
			}
		});
	}
	
	/**
	 * 删除地址
	 * @param id
	 */
	private void httpDeleteAddress(String id){
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();

			JSONObject info = new JSONObject();
			info.putOpt("id", id);

			jsonObject.putOpt("model", "MemberAddress");
			jsonObject.putOpt("function", "delAdderssApp");
			jsonObject.putOpt("info", info);

			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		utils.send(HttpMethod.POST, OApi.Appport_appMemberInfo, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				JLog.e("oshop", arg0.getMessage().toString());
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				httpGetAddressInfo(PrefUtils.getString(ReceiptAddressActivity.this, "userId", "1"));
			}
		});
	}
}
